<template>
	<view>
		<view class="registerBox">
			<image src="../../static/logo.png" class="imgPro" />

			<template v-if="true">
				<view class="itemBox">
					<i class="iconfont icon-ziyuan5"></i>
					<input name="" type="text" placeholder="手机号码" v-model="username" />
				</view>
				<view class="itemBox">
					<i class="iconfont icon-ziyuan"></i>
					<input name="" type="password" placeholder="密码" v-model="password" />
				</view>
				<view class="btnBox btnBox_1"><button name="" type="button" class="btn_1" @click="login">手机号登录</button></view>
			</template>
			<view class="btnBox">
				
				<!-- #ifdef H5 -->
				<button open-type="getUserInfo" @getuserinfo="wxH5Login" class="wxLogin btn_2">
					<text class="iconfont icon-weixin1"></text>
					微信登录
				</button>
				<!-- #endif -->
				
				<!-- #ifdef MP-WEIXIN -->
				<button open-type="getUserInfo" @getuserinfo="wxGetUserInfo" class="wxLogin btn_2">
					<text class="iconfont icon-weixin1"></text>
					微信登录
				</button>
				<!-- #endif -->

				<!-- <button name="" type="button" class="btn_2" @click="wx_login" >使用微信登录</button> -->
			</view>
			<view class="noPsd"><!-- <p>Forgot password ?</p> --></view>
		</view>
	</view>
</template>

<script>
import myconfig from '../../config.js';
import mycommon from '../../common.js';

export default {
	data() {
		return {
			password: '',
			username: ''
		};
	},
	
	onLoad(options) {
		
		//#ifdef H5
		if(!mycommon.isEmpty(options.token)){
			this.refreshOpenid(options.token)
		}
		//#endif
		
	},
	
	methods: {
		
		// 手机号登录
		login() {
			
			
			 
			
			
			this.$http.post('/shop.everyone/login', { username: this.username, password: this.password }).then(res => {
				
				//if(res.data)
				//return false;
				
				let token = res.data.data;

				if (token != '' || token != null) {
					
					uni.setStorageSync('token', res.data.data);
					uni.navigateTo({
						url:'../mine/mine'
					});
					
					// cookies.setCookie('token', token);

					//let redirect_url = this.$route.query.redirect;					 
				}
				return false;
			});
		},
		
		
		//#ifdef H5
		wxH5Login(){
			location.replace(myconfig.wxLoginUrl);
		},		
		refreshOpenid(token) {
		  // 1从url获取参数
		
		  // 2保存到cookies
		  if (token != null) {		
		
			uni.setStorageSync('token', token)
			uni.navigateTo({
				url:'../home/home'
			})			 
		  }
		}
		//#endif
		
		

		// 小程序登录
		//#ifdef MP-WEIXIN
		wxGetUserInfo: function(res) {
			let _that = this;
			if (!res.detail.iv) {
				uni.showToast({
					title: res.detail.errMsg,
					icon: 'none',
					duration: 5000
				});
				return false;
			}
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					res.detail['code'] = loginRes.code;

					console.log(res);

					//uni.showLoading();

					_that.$http.post('/shop.everyone/wx_app_login', res.detail).then(res => {
						console.log(res);

						if (res.data.code === 0) {
							uni.setStorageSync('token', res.data.data);
							uni.navigateBack();
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 5000
							});
							return false;
						}

						// _that.getMemberInfo();
					});
				}
			});
		}
		//#endif
	}
};
</script>

<style lang="scss">
.mainWarp {
	width: 100%;
	max-width: 512px;
	margin: 0 auto;
	background: #fff;
	height: 100%;
}

.registerBox {
	width: 100%;
	background-size: 100% 100%;
}

.imgPro {
	width: 100px;
	height: 100px;
	display: block;
	margin: 60rpx auto;
}

.itemBox {
	width: 75%;
	margin: 5px auto;
	overflow: hidden;
	height: 40px;
	line-height: 40px;
	border-bottom: 1px solid #d1d1d1;
	padding: 5px 0;
}
.itemBox i {
	width: 30px;
	height: 30px;
	float: left;
	margin-top: 5px;
	margin-right: 10px;
	font-size: 24px;
}
.itemBox input {
	width: 70%;
	float: left;
	height: 40px;
	line-height: 40px;
	outline: none;
	border: none;
	color: #3a3a3a;
	font-size: 16px;
	letter-spacing: 0.5px;
}
input::-webkit-input-placeholder {
	color: #3a3a3a;
}
input::-moz-placeholder {
	/* Mozilla Firefox 19+ */
	color: #3a3a3a;
}
input:-moz-placeholder {
	/* Mozilla Firefox 4 to 18 */
	color: #3a3a3a;
}
input:-ms-input-placeholder {
	/* Internet Explorer 10-11 */
	color: #3a3a3a;
}

.btnBox {
	width: 75%;
	margin: 0 auto;
	overflow: hidden;
	margin-top: 15px;
}
.btnBox i {
	font-size: 18px;
}
.btnBox.btnBox_1 {
	margin-top: 40px;
}
.btnBox button {
	width: 100%;
	height: 35px;
	line-height: 35px;
	cursor: pointer;
	color: #fff;
	border: none;
	border-radius: 3px;
	font-size: 16px;
	letter-spacing: 1px;
}
.btn_1 {
	background: #f9671e;
}
.btn_2 {
	background: #1aad19;
}

.noPsd {
	width: 75%;
	margin: 0 auto;
	overflow: hidden;
	margin-top: 40px;
}
.noPsd p {
	color: #8b8b8b;
	font-size: 14px;
	text-align: right;
}
</style>
